// Agent printer in project publicprintingwithjason

/* Initial beliefs and rules */

//the printer has supplies for only 3 printing requests
supplies(3).
//the printer status
status(ok).

/* Initial goals */

!start.

/* Plans */

//just letting the server know that this printer is ok
//and can receive printing requests
+!start : true
	<-
	.my_name(Me);
	.send(server, tell, printer(Me, ok)).

//when requested by the server, the printer will change its status to busy and print
//when done, the printer will update its supplies
@pprintfor[atomic]
+!printfor(C)[source(server)] : true 
	<-
	.my_name(Me);
	.send(server, tell, printer(Me, busy));
	-+status(busy);
	print(C);
	!updatesupplies.

//if there is no supplies the printer will ask the supplier for more supplies
//and also will let the server know that it is out of order and can't print
+!updatesupplies : supplies(X) & X <= 1
	<-
	-+supplies(0);
	.my_name(Me);
	.send(server, tell, printer(Me, outoforder));
	-+status(outoforder);
	updatestatus(outoforder);
	!!asksupplies.

//if the printer still have supplies, it will just reduce one of its beliefs
+!updatesupplies : true
	<-
	?supplies(X);
	-+supplies(X-1);
	.my_name(Me);
	.send(server, tell, printer(Me, ok));
	-+status(ok).

//in order to ask for supplies, the printer asks the supplier
+!asksupplies : true
	<-
	.my_name(Me);
	.send(supplier, achieve, supply(Me)).

//when the supplier delievers supplies, the printer will update its beliefs
//and let the server know that it is ok
+supplies(X)[source(supplier)] : true 
	<-
	-supplies(X)[source(supplier)];
	-+supplies(X);
	updatestatus(ok);
	.my_name(Me);
	.send(server, tell, printer(Me, ok));
	-+status(ok).